package com.example.myapplication.ui.dashboard;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.MvActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.MVAdapter;
import com.example.myapplication.api.MusicApi;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.BaseResponseAlbum;
import com.example.myapplication.entity.TopMV;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DashboardFragment extends Fragment {
    private RecyclerView recyclerView;
    private Context context;
    private MVAdapter adapter;
    private List<TopMV> mvList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(container.getContext()).inflate(R.layout.fragment_dashboard, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        context = getContext();
        recyclerView = view.findViewById(R.id.recycler_view);
        GridLayoutManager manager = new GridLayoutManager(context, 2);
        recyclerView.setLayoutManager(manager);
        adapter = new MVAdapter(context);
        recyclerView.setAdapter(adapter);

        loadData();

        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(getActivity(), MvActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("mvID", mvList.get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void loadData(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);

        Call<BaseResponseAlbum> call = api.getMV();
        call.enqueue(new Callback<BaseResponseAlbum>() {
            @Override
            public void onResponse(Call<BaseResponseAlbum> call, Response<BaseResponseAlbum> response) {
                BaseResponseAlbum baseResponse = response.body();
                mvList = baseResponse.getData();
                adapter.setData(mvList);
            }

            @Override
            public void onFailure(Call<BaseResponseAlbum> call, Throwable t) {
                t.printStackTrace();
            }
        });

    }
}